using System;
using System.Data;
using System.Configuration;
using System.Collections;
using System.Web;
using System.Web.Security;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Web.UI.WebControls.WebParts;
using System.Web.UI.HtmlControls;
using FetCo.Data;
using ADESSA.Business;
using ADESSA.Entities;
using FetCo.Framework;
using FetCo.Web;

public partial class Admin_ProductCatSub : System.Web.UI.Page
{
    #region Variable
    protected long Id
    {
        get
        {
            object o = ViewState["Id"];
            if (o != null)
                return (long)o;
            return long.MinValue;
        }
        set
        {
            ViewState["Id"] = value;
        }
    }

    #endregion

    protected void Page_Load(object sender, EventArgs e)
    {
        if (IsPostBack)
            return;
        if (Request.QueryString["id"] != null)
        {
            this.Id = long.Parse(Request.QueryString["id"].ToString());
        }
        LoadDll();
        LoadData();
        LoadCurent();

    }
    #region LoadCurent
    #region Load dropdownlist
    private void LoadDll()
    {
        ddlProCat.Items.Clear();
        ddlProCat.Items.Add(new ListItem("- - - - Select Category - - - -", "0"));
        Criteria cri = new Criteria();
        cri.add("[" + ProductCat._Table + "].[" + ProductCatColumns.CatLevel + "]", 0, Condition.EQUAL, Condition.AND);
        ProductCatCollection _ProductCatCollection = ProductCatService.GetProductCatListByParam("[" + ProductCat._Table + "].[" + ProductCatColumns.SortOrder + "]", OrderDirection.ASC, cri.Criter);
        foreach (ProductCat _ProductCat in _ProductCatCollection)
        {
            ddlProCat.Items.Add(new ListItem(_ProductCat.Name, _ProductCat.ProCateID.ToString()));
        }
    }
    #endregion
    #region LoadCurent
    private void LoadCurent()
    {
        HtmlGenericControl ulProduct = (HtmlGenericControl)Master.FindControl("ulProduct");
        ulProduct.Attributes.Add("class", "current");
        HtmlControl divSelectSubPro = (HtmlControl)Master.FindControl("divSelectSubPro");
        divSelectSubPro.Attributes.Add("class", "select_sub show");
        HtmlGenericControl liSubCatPro = (HtmlGenericControl)Master.FindControl("liSubCatPro");
        liSubCatPro.Attributes.Add("class", "sub_show");

    }
    #endregion
    #endregion
    #region LoadData
    private void LoadData()
    {
        ADESSA.Entities.ProductCat _ProductCat = new ADESSA.Entities.ProductCat();
        if (this.Id != long.MinValue && this.Id != 0)
        {
            _ProductCat = ProductCatService.GetProductCat(this.Id);
            if (_ProductCat != null)
            {
                txtName.Value = _ProductCat.Name;
                txtOrder.Value = _ProductCat.SortOrder.ToString();
                txtDes.Value = _ProductCat.Description;
                ddlProCat.SelectedValue = _ProductCat.Parrentid.ToString();
            }
        }
    }
    #endregion
    #region VoidSave and Reset
    protected void btnsave_Click(object sender, EventArgs e)
    {
        ADESSA.Entities.ProductCat _ProductCat = new ADESSA.Entities.ProductCat();
        if (this.Id != long.MinValue && this.Id != 0)
        {
            _ProductCat = ProductCatService.GetProductCat(this.Id);
        }
        _ProductCat.Name = txtName.Value.Trim();
        _ProductCat.SortOrder = Utility.ParseToBigint(txtOrder.Value.Trim());
        _ProductCat.Description = txtDes.Value.Trim();

        _ProductCat.Parrentid = Utility.ParseToBigint(ddlProCat.SelectedValue);
        _ProductCat.CatLevel = 1;
        if (this.Id != long.MinValue && this.Id != 0)
        {
            ProductCatService.UpdateProductCat(_ProductCat);
            Response.Redirect("ProductCatSubList.aspx");
        }
        else
        {
            _ProductCat.Enable = true;
            ProductCatService.CreateProductCat(_ProductCat);
            Response.Redirect("ProductCatSubList.aspx");
        }
    }
    protected void btnexit_Click(object sender, EventArgs e)
    {
        Response.Redirect("ProductCatSubList.aspx");
    }
    #endregion
}
